package com.zzw.util;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.PopupWindow;
import android.widget.TextView;
import android.widget.Toast;

import com.google.android.gms.appindexing.Action;
import com.google.android.gms.appindexing.AppIndex;
import com.google.android.gms.common.api.GoogleApiClient;
import com.zzw.activity.MainActivity;
import com.zzw.activity.QuestionActivity;
import com.zzw.activity.R;
import com.zzw.activity.SearchActivity;

import java.util.ArrayList;
import java.util.List;

public class BaseActivity extends AppCompatActivity {

    private TextView topTitle;
    private Button setting;
    private PopupWindow win;

    private Class[] target = new Class[]{MainActivity.class, QuestionActivity.class, SearchActivity.class};

    private Button[] bottomBtns = new Button[3];

    // 未被选中时按钮的图片
    private int[] bottomImages = new int[]{R.drawable.bottombtn01b, R.drawable.bottombtn02b, R.drawable.bottombtn03b};

    // 被选中的按钮图片
    private int[] bottoImagesSelected = new int[]{R.drawable.bottombtn01a, R.drawable.bottombtn02a, R.drawable.bottombtn03a};

    private int i;

    private List<Activity> allActivities = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        allActivities.add(this);
        super.onCreate(savedInstanceState);
    }

    protected void init(String title, int index) {
        topTitle = (TextView) findViewById(R.id.top_title);
        topTitle.setText(title);

        bottomBtns[0] = (Button) findViewById(R.id.bottom_btn_01);
        bottomBtns[1] = (Button) findViewById(R.id.bottom_btn_02);
        bottomBtns[3] = (Button) findViewById(R.id.bottom_btn_03);

        for (i = 0; i < bottomBtns.length; i++) {
            final int temp = i;   // 防止数组下标越界
            if (i == index) {
                bottomBtns[i].setBackgroundResource(bottoImagesSelected[i]);
            } else {
                bottomBtns[i].setBackgroundResource(bottomImages[i]);

                bottomBtns[i].setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Intent intent = new Intent(BaseActivity.this, target[temp]);
                        startActivity(intent);
                    }
                });
            }
        }

        setting = (Button) findViewById(R.id.setting);
        setting.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                win = new PopupWindow(Globals.SCREEN_WIDTH / 4, Globals.SCREEN_HEIGHT / 6);
                View view = LayoutInflater.from(BaseActivity.this).inflate(R.layout.win_list,null);
                win.setContentView(view);

                if(win.isShowing()){
                    win.dismiss();
                }else{
                    win.showAsDropDown(setting); // 将弹出窗口放置在setting按钮下方
                }
            }
        });

        TextView version = (TextView) findViewById(R.id.win_version);
        TextView about = (TextView) findViewById(R.id.win_about);
        TextView exit  = (TextView) findViewById(R.id.win_exit);

        version.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                win.dismiss();
                Toast.makeText(BaseActivity.this,"当前为1.0.1版本",Toast.LENGTH_SHORT).show();
            }
        });

        about.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                win.dismiss();
                AlertDialog.Builder builder = new AlertDialog.Builder(BaseActivity.this);
                builder.setTitle("提示信息");
                builder.setMessage("此应用为北航团队制作");
                builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {

                    }
                });
                builder.create();
            }
        });

        exit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                win.dismiss();
                for (Activity a : allActivities) {
                    a.finish();
                }
            }
        });
    }

    @Override
    protected void onDestroy() {
        allActivities.remove(this);
        super.onDestroy();
    }
}
